import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/Users.vue'
import Roles from '../components/Roles.vue'
import Rights from '../components/Rights.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      }, {
        path: '/users',
        component: Users
      },
      {
        path: '/rights',
        component: Rights
      }
      ,
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/categories',
        component:
          () => {
            return import('../components/Goods/Categories.vue')
          }
      }
      , {
        path: '/params',
        component:
          () => {
            return import('../components/Goods/Params.vue')
          }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 配置路由守卫
// beforeEach参数顺序固定的,要注意,
router.beforeEach((to, from, next) => {
  // 从登录的地址过来直接通行
  if (to.path === '/login') {
    return next()
  }
  // 否则获取token校验
  else {
    const tokenStr = window.sessionStorage.getItem('dianshangToken')
    if (!tokenStr) {
      return next('/login')
    }
    next()
  }

})

export default router
